package com.cy3;

/**
 * 自己实现基于双向链表的linkedlist
 *
 * @author CY
 * @date 2022/10/12 17:36
 **/
public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    ListNode head = null;
    ListNode tail = null;

    public void display() {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        if (head == null) {
            return 0;
        }
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void addFirst(int val) {
        ListNode listNode = new ListNode(val);
        if (head == null) {
            this.head = listNode;
            this.tail = listNode;
        } else {

            listNode.next = head;
            this.head.prev = listNode;
            this.head = listNode;
        }
    }

    public void addLast(int val) {
        ListNode node = new ListNode(val);
        if (this.head == null) {
            this.head = node;
            this.tail = node;
        } else {
            tail.next = node;
            node.prev = tail;
            tail = node;
        }
    }

    public void addIndex(int index, int val) {
        if (index < 0 || index > this.size()) {
            return;
        }
        if (index == 0) {
            addFirst(index);
            return;
        }
        if (index == size()) {
            addLast(index);
            return;
        }
        ListNode cur = head;
        while ((index--) > 0) {
            cur = cur.next;
        }
        ListNode node = new ListNode(val);
        node.prev = cur.prev;
        node.next = cur;
        node.next.prev = node;
        node.prev.next = node;
    }

    //删除第一次出现关键字为key的节点
    public boolean remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur.prev == null) {
                    //删除头节点
                    head = cur.next;
                    head.prev = null;
                } else if (cur.next == null) {
                    //删除尾节点
                    tail = cur.prev;
                    tail.next = null;
                } else {
                    //删除一般节点
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return true;
            } else {
                cur = cur.next;
            }
        }
        return false;
    }

    //删除所有值为key的节点
    public boolean removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur.prev == null) {
                    //删除头节点
                    head = cur.next;
                    head.prev = null;
                } else if (cur.next == null) {
                    //删除尾节点
                    tail = cur.prev;
                    tail.next = null;
                } else {
                    //删除一般节点
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
        if (cur == null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean contains(int key) {
        if (head == null) {
            return false;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
//在清空双向链表的时，我们需要一个一个节点去置空，结束收还需有将头尾节点置空；
    public void clear() {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        //置空头尾节点
        head = null;
        tail = null;
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(1);
        myLinkedList.addLast(2);
        myLinkedList.addLast(3);
        myLinkedList.addLast(4);
        myLinkedList.addLast(5);
        myLinkedList.addLast(5);
        myLinkedList.addLast(5);
        myLinkedList.addLast(5);
        myLinkedList.addIndex(2, 45);
        myLinkedList.addIndex(2, 45);
        myLinkedList.addIndex(2, 45);
        myLinkedList.addLast(5);
        myLinkedList.addLast(5);

        myLinkedList.display();

        System.out.println(myLinkedList.remove(5));
        myLinkedList.display();
        System.out.println(myLinkedList.contains(5));


    }
}
